package com.zhht.htzx.adapter;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.bean.OrderListBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.ui.adapter.SimpleDataBindingPositionAdapter;
import com.nsgf.library.ui.adapter.ViewHolder;
import com.nsgf.library.utils.BaseAlterDialog;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ItemOrderBinding;
import com.zhht.htzx.databinding.ItemOrderProductBinding;

/**
 * 订单列表适配器
 */
public class OrderAdapter extends SimpleDataBindingPositionAdapter<OrderListBean.ListBean, ItemOrderBinding> {

    public OrderAdapter(Context context) {
        super(context, R.layout.item_order, new DiffUtil.ItemCallback<OrderListBean.ListBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull OrderListBean.ListBean oldItem,
                                           @NonNull OrderListBean.ListBean newItem) {
                return oldItem.getId().equals(newItem.getId());
            }

            @Override
            public boolean areContentsTheSame(@NonNull OrderListBean.ListBean oldItem,
                                              @NonNull OrderListBean.ListBean newItem) {
                // 首先比较status
                if (oldItem.getStatus() != newItem.getStatus()) {
                    Log.d("OrderAdapter", "status不同: old=" + oldItem.getStatus() + ", new=" + newItem.getStatus());
                    return false;
                }

                // 比较items列表
                if (oldItem.getItems() == null && newItem.getItems() == null) {
                    return true;
                }
                if (oldItem.getItems() == null || newItem.getItems() == null) {
                    Log.d("OrderAdapter", "items一个为null");
                    return false;
                }
                if (oldItem.getItems().size() != newItem.getItems().size()) {
                    Log.d("OrderAdapter", "items大小不同: old=" + oldItem.getItems().size() + ", new=" + newItem.getItems().size());
                    return false;
                }

                // 比较每个item的内容
                for (int i = 0; i < oldItem.getItems().size(); i++) {
                    OrderListBean.ListBean.ItemsBean oldItemsBean = oldItem.getItems().get(i);
                    OrderListBean.ListBean.ItemsBean newItemsBean = newItem.getItems().get(i);

                    if (oldItemsBean.getId() != newItemsBean.getId() ||
                            oldItemsBean.getAfterSaleStatus() != newItemsBean.getAfterSaleStatus()) {
                        Log.d("OrderAdapter", "item内容不同: position=" + i
                                + ", oldId=" + oldItemsBean.getId() + ", newId=" + newItemsBean.getId()
                                + ", oldAfterSaleStatus=" + oldItemsBean.getAfterSaleStatus()
                                + ", newAfterSaleStatus=" + newItemsBean.getAfterSaleStatus());
                        return false;
                    }
                }

                return true;
            }
        });
    }


    private OnItemClickListener listener;

    public void setOnClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onBindItem(ItemOrderBinding binding, OrderListBean.ListBean item, ViewHolder holder, int position) {

        binding.setVm(item);

        binding.recyclerView.setNestedScrollingEnabled(false);
        binding.setAdapter(new SimpleDataBindingAdapter<OrderListBean.ListBean.ItemsBean,
                ItemOrderProductBinding>(mContext,
                R.layout.item_order_product,
                new DiffUtil.ItemCallback<OrderListBean.ListBean.ItemsBean>() {
                    @Override
                    public boolean areItemsTheSame(@NonNull OrderListBean.ListBean.ItemsBean oldItem, @NonNull OrderListBean.ListBean.ItemsBean newItem) {
                        return oldItem.getId() == newItem.getId();
                    }

                    @Override
                    public boolean areContentsTheSame(@NonNull OrderListBean.ListBean.ItemsBean oldItem, @NonNull OrderListBean.ListBean.ItemsBean newItem) {
                        return oldItem.getAfterSaleStatus() == newItem.getAfterSaleStatus() &&
                                oldItem.getId() == newItem.getId();
                    }
                }) {
            @Override
            protected void onBindItem(ItemOrderProductBinding binding,
                                      OrderListBean.ListBean.ItemsBean childItem,
                                      RecyclerView.ViewHolder holder) {
                binding.setVm(childItem);

                if (item.getStatus() == 10 && childItem.getAfterSaleStatus() == 0) {
                    binding.tvItemOrderRefund.setVisibility(VISIBLE);
                } else {
                    binding.tvItemOrderRefund.setVisibility(GONE);
                }
                setOnItemClickListener(new OnItemClickListener<OrderListBean.ListBean.ItemsBean>() {
                    @Override
                    public void onItemClick(int viewId, OrderListBean.ListBean.ItemsBean bean, int position) {
                        listener.onItemClick(item);
                    }
                });

                binding.tvItemOrderRefund.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        new BaseAlterDialog(mContext, R.style.dialogstyle, "确定要申请退款吗？",
                                "确定",
                                "取消",
                                (dialog, confirm) -> {

                                    dialog.dismiss();
                                    listener.onRefundClick(item, childItem);
                                }, new BaseAlterDialog.OnCloseListener() {
                            @Override
                            public void onCancleClick(Dialog dialog, boolean confirm) {
                                dialog.dismiss();
                            }
                        }).show();

                    }
                });

            }
        });


        binding.tvItemOrderDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new BaseAlterDialog(mContext, R.style.dialogstyle, "确定要删除订单吗？",
                        "确定",
                        "取消",
                        (dialog, confirm) -> {

                            dialog.dismiss();
                            listener.onDeleteClick(item, position);

                        }, new BaseAlterDialog.OnCloseListener() {
                    @Override
                    public void onCancleClick(Dialog dialog, boolean confirm) {
                        dialog.dismiss();
                    }
                }).show();
            }
        });

        binding.tvItemOrderCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onCancelClick(item, position);
            }
        });
        binding.tvItemOrderConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onConfirmDeliveryClick(item);
            }
        });

    }

    public interface OnItemClickListener {
        void onItemClick(OrderListBean.ListBean order);

        void onDeleteClick(OrderListBean.ListBean order, int position);

        void onCancelClick(OrderListBean.ListBean order, int position);

        void onConfirmDeliveryClick(OrderListBean.ListBean order);

        void onRefundClick(OrderListBean.ListBean order, OrderListBean.ListBean.ItemsBean childItem);
    }
} 